package name.fowler.refactoring.simplestore;

import java.util.ArrayList;
import java.util.List;

public class Customer {
	private String m_Name;
	private List<Rental> m_Rentals = new ArrayList<Rental>();

	public class Statement {
		class Entry {
			public final String m_movieTitle;
			public final double m_amount;

			Entry(final String movieTitle, final double amount) {
				m_movieTitle = movieTitle;
				m_amount = amount;
			}

			public final String getMovieTitle() {
				return m_movieTitle;
			}

			public final double getAmount() {
				return m_amount;
			}
		}

		private double m_totalAmount;
		private int m_frequentRenterPoints;
		private final ArrayList<Entry> m_entries;

		public Statement() {
			m_entries = new ArrayList<Entry>();
		}

		public void addEntry(final String movieTitle, double amount,
				int frequentRenterPoints) {
			m_entries.add(new Entry(movieTitle, amount));
			m_totalAmount += amount;
			m_frequentRenterPoints += frequentRenterPoints;
		}

		public final ArrayList<Entry> getEntries() {
			return m_entries;
		}

		public final double getTotalAmount() {
			return m_totalAmount;
		}

		public final int getFrequentRenterPoints() {
			return m_frequentRenterPoints;
		}

		public final String toString() {
			String result = new String();
			for (Entry each : m_entries) {
				result += "\t" + each.getMovieTitle() + "\t" + each.getAmount()
						+ "\n";
			}

			// Add footer lines
			result += "Amount owed is " + getTotalAmount() + "\n";
			result += "You earned " + getFrequentRenterPoints()
					+ " frequent renter points.";
			return result;
		}
	}

	public Customer(String name) {
		m_Name = name;
	}

	public String getName() {
		return m_Name;
	}

	public void addRental(Rental arg) {
		m_Rentals.add(arg);
	}

	public String Statement() {
		String result = "Rental record for " + m_Name + "\n";
		result += evaluateStatement();
		return result;
	}

	public final Statement evaluateStatement() {
		final Statement result = new Statement();

		for (Rental each : m_Rentals) {
			final String movieTitle = each.getMovie().getTitle();
			final double amount = each.getAmount();
			final boolean twoDayNewReleaseRental = (each.getMovie()
					.getPriceCode() == PriceCodes.NewRelease)
					&& (each.getDaysRented() > 1);
			final int frequentRenterPoints = twoDayNewReleaseRental ? 2 : 1;

			result.addEntry(movieTitle, amount, frequentRenterPoints);

		}

		return result;
	}
}
